<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li class="active"><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 单元测试">1. 单元测试</a><ul><li><a href="#t11.1 TDD 测试驱动开发">1.1 TDD 测试驱动开发</a></li><li><a href="#t21.2 BDD 行为驱动开发">1.2 BDD 行为驱动开发</a></li></ul></li><li><a href="#t32. 工具">2. 工具</a><ul><li><a href="#t42.1 chai">2.1 chai</a></li><li><a href="#t52.2 supertest">2.2 supertest</a></li></ul></li><li><a href="#t63. egg.js">3. egg.js</a><ul><li><a href="#t73.1 配置命令">3.1 配置命令</a></li><li><a href="#t83.2 执行顺序">3.2 执行顺序</a></li><li><a href="#t93.2 测试接口返回值">3.2 测试接口返回值</a></li><li><a href="#t103.3 测试控制器">3.3 测试控制器</a></li><li><a href="#t113.4 测试服务">3.4 测试服务</a></li><li><a href="#t123.5 测试扩展">3.5 测试扩展</a><ul><li><a href="#t133.5.1 application">3.5.1 application</a></li><li><a href="#t143.5.2 context">3.5.2 context</a></li><li><a href="#t153.5.3 request">3.5.3 request</a></li><li><a href="#t163.5.4 response">3.5.4 response</a></li><li><a href="#t173.5.5 helper">3.5.5 helper</a></li></ul></li></ul></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x5355;&#x5143;&#x6D4B;&#x8BD5;">1. &#x5355;&#x5143;&#x6D4B;&#x8BD5; <a href="#t01. &#x5355;&#x5143;&#x6D4B;&#x8BD5;"> # </a></h2>
<h3 id="t11.1 TDD &#x6D4B;&#x8BD5;&#x9A71;&#x52A8;&#x5F00;&#x53D1;">1.1 TDD &#x6D4B;&#x8BD5;&#x9A71;&#x52A8;&#x5F00;&#x53D1; <a href="#t11.1 TDD &#x6D4B;&#x8BD5;&#x9A71;&#x52A8;&#x5F00;&#x53D1;"> # </a></h3>
<p>TDD&#x6307;&#x7684;&#x662F;Test Drive Development&#xFF0C;&#x5F88;&#x660E;&#x663E;&#x7684;&#x610F;&#x601D;&#x662F;&#x6D4B;&#x8BD5;&#x9A71;&#x52A8;&#x5F00;&#x53D1;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4ECE;&#x6D4B;&#x8BD5;&#x7684;&#x89D2;&#x5EA6;&#x6765;&#x68C0;&#x9A8C;&#x6574;&#x4E2A;&#x9879;&#x76EE;&#x3002;&#x5927;&#x6982;&#x7684;&#x6D41;&#x7A0B;&#x662F;&#x5148;&#x9488;&#x5BF9;&#x6BCF;&#x4E2A;&#x529F;&#x80FD;&#x70B9;&#x62BD;&#x8C61;&#x51FA;&#x63A5;&#x53E3;&#x4EE3;&#x7801;&#xFF0C;&#x7136;&#x540E;&#x7F16;&#x5199;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#xFF0C;&#x8FD0;&#x884C;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#xFF0C;&#x5FAA;&#x73AF;&#x6B64;&#x8FC7;&#x7A0B;&#xFF0C;&#x76F4;&#x5230;&#x6574;&#x4E2A;&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x90FD;&#x901A;&#x8FC7;&#x3002;</p>
<h3 id="t21.2 BDD &#x884C;&#x4E3A;&#x9A71;&#x52A8;&#x5F00;&#x53D1;">1.2 BDD &#x884C;&#x4E3A;&#x9A71;&#x52A8;&#x5F00;&#x53D1; <a href="#t21.2 BDD &#x884C;&#x4E3A;&#x9A71;&#x52A8;&#x5F00;&#x53D1;"> # </a></h3>
<ul>
<li>BDD&#x6307;&#x7684;&#x662F;Behavior Drive Development&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x884C;&#x4E3A;&#x9A71;&#x52A8;&#x5F00;&#x53D1;&#x3002;</li>
<li>&#x5728;TDD&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5E76;&#x4E0D;&#x80FD;&#x5B8C;&#x5168;&#x4FDD;&#x8BC1;&#x6839;&#x636E;&#x8BBE;&#x8BA1;&#x6240;&#x7F16;&#x5199;&#x7684;&#x6D4B;&#x8BD5;&#x5C31;&#x662F;&#x7528;&#x6237;&#x6240;&#x671F;&#x671B;&#x7684;&#x529F;&#x80FD;&#x3002;</li>
<li>BDD&#x5C06;&#x8FD9;&#x4E00;&#x90E8;&#x5206;&#x7B80;&#x5355;&#x548C;&#x81EA;&#x7136;&#x5316;&#xFF0C;&#x7528;&#x81EA;&#x7136;&#x8BED;&#x8A00;&#x6765;&#x63CF;&#x8FF0;&#xFF0C;&#x8BA9;&#x5F00;&#x53D1;&#x3001;&#x6D4B;&#x8BD5;&#x3001;BA&#x4EE5;&#x53CA;&#x5BA2;&#x6237;&#x90FD;&#x80FD;&#x5728;&#x8FD9;&#x4E2A;&#x57FA;&#x7840;&#x4E0A;&#x8FBE;&#x6210;&#x4E00;&#x81F4;&#x3002;</li>
</ul>
<h2 id="t32. &#x5DE5;&#x5177;">2. &#x5DE5;&#x5177; <a href="#t32. &#x5DE5;&#x5177;"> # </a></h2>
<ul>
<li>&#x5355;&#x5143;&#x6D4B;&#x8BD5;&#x6846;&#x67B6; mocha <a href="http://mochajs.org">http://mochajs.org</a></li>
<li>&#x65AD;&#x8A00; chai http:/chaijs.com</li>
<li>&#x4EE3;&#x7406;HTTP&#x8BF7;&#x6C42; supertest <a href="https://npmjs.com/package/supertest">https://npmjs.com/package/supertest</a></li>
</ul>
<h3 id="t42.1 chai">2.1 chai <a href="#t42.1 chai"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { expect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;chai&apos;</span>);
<span class="hljs-comment">//mocha -t 5000</span>
describe(<span class="hljs-string">&apos;zftest&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    it(<span class="hljs-string">&apos;#1&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        expect(<span class="hljs-number">1</span> + <span class="hljs-number">1</span>).to.be.equal(<span class="hljs-number">2</span>);
    })
    it(<span class="hljs-string">&apos;#2&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">done</span>) </span>{
        setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            expect(<span class="hljs-number">1</span> + <span class="hljs-number">1</span>).to.be.equal(<span class="hljs-number">2</span>);
            done();
        }, <span class="hljs-number">1000</span>);
    })
    it(<span class="hljs-string">&apos;#3&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">resolve</span>) </span>{
            setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
                expect(<span class="hljs-number">1</span> + <span class="hljs-number">1</span>).to.be.equal(<span class="hljs-number">2</span>);
                resolve();
            }, <span class="hljs-number">3000</span>);
        });
    });
});

</code></pre>
<h3 id="t52.2 supertest">2.2 supertest <a href="#t52.2 supertest"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> express = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;express&apos;</span>);
<span class="hljs-keyword">const</span> app = express();
app.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">req, res</span>) </span>{
    res.status(<span class="hljs-number">200</span>).json({ <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span> });
});
app.listen(<span class="hljs-number">8080</span>);
<span class="hljs-built_in">module</span>.exports = app;
</code></pre>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> app = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;../src/app&apos;</span>);
<span class="hljs-keyword">const</span> request = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;supertest&apos;</span>);
describe(<span class="hljs-string">&apos;app&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    it(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">done</span>) </span>{
        request(app)
            .get(<span class="hljs-string">&apos;/&apos;</span>)
            .expect(<span class="hljs-string">&apos;Content-Type&apos;</span>, /json/)
            .expect(<span class="hljs-string">&apos;Content-Length&apos;</span>, <span class="hljs-string">&quot;15&quot;</span>)
            .expect(<span class="hljs-number">200</span>)
            .end(done);
    });
});
</code></pre>
<h2 id="t63. egg.js">3. egg.js <a href="#t63. egg.js"> # </a></h2>
<ul>
<li>&#x7EA6;&#x5B9A;test&#x76EE;&#x5F55;&#x4E3A;&#x5B58;&#x653E;&#x6240;&#x6709;&#x7684;&#x6D4B;&#x8BD5;&#x811A;&#x672C;&#x7684;&#x76EE;&#x5F55;</li>
<li>&#x6D4B;&#x8BD5;&#x811A;&#x672C;&#x6587;&#x4EF6;&#x7EDF;&#x4E00;&#x6309; ${filename}.test.js &#x547D;&#x540D;&#xFF0C;&#x5FC5;&#x987B;&#x4EE5; .test.js &#x4F5C;&#x4E3A;&#x6587;&#x4EF6;&#x540E;&#x7F00;&#x3002;</li>
</ul>
<h3 id="t73.1 &#x914D;&#x7F6E;&#x547D;&#x4EE4;">3.1 &#x914D;&#x7F6E;&#x547D;&#x4EE4; <a href="#t73.1 &#x914D;&#x7F6E;&#x547D;&#x4EE4;"> # </a></h3>
<pre><code class="lang-js">{
  <span class="hljs-string">&quot;scripts&quot;</span>: {
    <span class="hljs-string">&quot;test&quot;</span>: <span class="hljs-string">&quot;egg-bin test&quot;</span>
  }
}
</code></pre>
<h3 id="t83.2 &#x6267;&#x884C;&#x987A;&#x5E8F;">3.2 &#x6267;&#x884C;&#x987A;&#x5E8F; <a href="#t83.2 &#x6267;&#x884C;&#x987A;&#x5E8F;"> # </a></h3>
<pre><code class="lang-js">describe(<span class="hljs-string">&apos;exec order&apos;</span>, () =&gt; {
    before(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span>));
    before(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">2</span>));
    after(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">6</span>));
    beforeEach(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">3</span>));
    afterEach(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-number">5</span>));
    it(<span class="hljs-string">&apos;should work&apos;</span>, () =&gt; <span class="hljs-built_in">console</span>.log(<span class="hljs-number">4</span>));
});
</code></pre>
<h3 id="t93.2 &#x6D4B;&#x8BD5;&#x63A5;&#x53E3;&#x8FD4;&#x56DE;&#x503C;">3.2 &#x6D4B;&#x8BD5;&#x63A5;&#x53E3;&#x8FD4;&#x56DE;&#x503C; <a href="#t93.2 &#x6D4B;&#x8BD5;&#x63A5;&#x53E3;&#x8FD4;&#x56DE;&#x503C;"> # </a></h3>
<p><a href="https://github.com/visionmedia/supertest">supertest</a></p>
<pre><code class="lang-js">it(<span class="hljs-string">&apos;promise 200&apos;</span>, () =&gt; {
    <span class="hljs-keyword">return</span> app.httpRequest()
        .get(<span class="hljs-string">&apos;/&apos;</span>)
        .expect(<span class="hljs-number">200</span>)
});

it(<span class="hljs-string">&apos;callback 200&apos;</span>, done =&gt; {
    app.httpRequest()
        .get(<span class="hljs-string">&apos;/&apos;</span>)
        .expect(<span class="hljs-number">200</span>, done);
});

it(<span class="hljs-string">&apos;done 200&apos;</span>, done =&gt; {
    app.httpRequest()
        .get(<span class="hljs-string">&apos;/&apos;</span>)
        .expect(<span class="hljs-number">200</span>)
        .end(done)
});

it(<span class="hljs-string">&apos;await 200&apos;</span>, <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">await</span> app.httpRequest()
        .get(<span class="hljs-string">&apos;/&apos;</span>)
        .expect(<span class="hljs-number">200</span>);
});
</code></pre>
<h3 id="t103.3 &#x6D4B;&#x8BD5;&#x63A7;&#x5236;&#x5668;">3.3 &#x6D4B;&#x8BD5;&#x63A7;&#x5236;&#x5668; <a href="#t103.3 &#x6D4B;&#x8BD5;&#x63A7;&#x5236;&#x5668;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, mock, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test/controller/home.test.js&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    describe(<span class="hljs-string">&apos;GET /&apos;</span>, () =&gt; {
        it(<span class="hljs-string">&apos;should 200 and get body&apos;</span>, () =&gt; {
            <span class="hljs-keyword">return</span> app.httpRequest()
                .get(<span class="hljs-string">&apos;/&apos;</span>)
                .expect(<span class="hljs-number">200</span>)
                .expect(<span class="hljs-string">&apos;hello&apos;</span>)
        })

        it(<span class="hljs-string">&apos;should 200 and get reqeust body&apos;</span>, () =&gt; {
            app.mockCsrf();
            <span class="hljs-keyword">return</span> app.httpRequest()
                .post(<span class="hljs-string">&apos;/post&apos;</span>)
                .type(<span class="hljs-string">&apos;form&apos;</span>)
                .send({ <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span> })
                .expect(<span class="hljs-number">200</span>)
                .expect({ <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span> })
        })
    });
});
</code></pre>
<h3 id="t113.4 &#x6D4B;&#x8BD5;&#x670D;&#x52A1;">3.4 &#x6D4B;&#x8BD5;&#x670D;&#x52A1; <a href="#t113.4 &#x6D4B;&#x8BD5;&#x670D;&#x52A1;"> # </a></h3>
<p>Service &#x76F8;&#x5BF9;&#x4E8E; Controller &#x6765;&#x8BF4;&#xFF0C;&#x6D4B;&#x8BD5;&#x8D77;&#x6765;&#x4F1A;&#x66F4;&#x52A0;&#x7B80;&#x5355;&#xFF0C; &#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; ctx&#xFF0C;&#x7136;&#x540E;&#x901A;&#x8FC7; ctx.service.${serviceName} &#x62FF;&#x5230; Service &#x5B9E;&#x4F8B;&#xFF0C; &#x7136;&#x540E;&#x8C03;&#x7528; Service &#x65B9;&#x6CD5;&#x5373;&#x53EF;&#x3002;
\app\service\user.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { Service } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg&apos;</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserService</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Service</span> </span>{
    <span class="hljs-keyword">async</span> create(user) {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.ctx.model.User.create(user);
    }
    <span class="hljs-keyword">async</span> get(username) {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">await</span> <span class="hljs-keyword">this</span>.ctx.model.User.findOne({ username });
    }
}
<span class="hljs-built_in">module</span>.exports = UserService;
</code></pre>
<p>\test\service\user.test.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;get name&apos;</span>, () =&gt; {
    it(<span class="hljs-string">&apos;create user&apos;</span>, <span class="hljs-keyword">async</span> () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext();
        <span class="hljs-keyword">const</span> doc = <span class="hljs-keyword">await</span> ctx.service.user.create({ <span class="hljs-attr">username</span>: <span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-attr">password</span>: <span class="hljs-string">&apos;123456&apos;</span>, <span class="hljs-attr">email</span>: <span class="hljs-string">&apos;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="46770637376825292b">[email&#160;protected]</a>&apos;</span> });
        assert(doc);
        assert(doc.username == <span class="hljs-string">&apos;zfpx&apos;</span>);
    });
    it(<span class="hljs-string">&apos;get user&apos;</span>, <span class="hljs-keyword">async</span> () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext();
        <span class="hljs-keyword">const</span> doc = <span class="hljs-keyword">await</span> ctx.service.user.get(<span class="hljs-string">&apos;zfpx&apos;</span>);
        assert(doc);
        assert(doc.username == <span class="hljs-string">&apos;zfpx&apos;</span>);
    });
});
</code></pre>
<h3 id="t123.5 &#x6D4B;&#x8BD5;&#x6269;&#x5C55;">3.5 &#x6D4B;&#x8BD5;&#x6269;&#x5C55; <a href="#t123.5 &#x6D4B;&#x8BD5;&#x6269;&#x5C55;"> # </a></h3>
<h4 id="t133.5.1 application">3.5.1 application <a href="#t133.5.1 application"> # </a></h4>
<p>app\extend\application.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    get name() {
        <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;app-name&apos;</span>;
    }
}
</code></pre>
<p>\test\extend\application.test.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test app&apos;</span>, () =&gt; {
    it(<span class="hljs-string">&apos;test app&apos;</span>, () =&gt; {
        assert(app.name == <span class="hljs-string">&apos;app-name&apos;</span>);
    });
});
</code></pre>
<h4 id="t143.5.2 context">3.5.2 context <a href="#t143.5.2 context"> # </a></h4>
<p>app\extend\context.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    get isXHR() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.get(<span class="hljs-string">&apos;X-Requested-With&apos;</span>) == <span class="hljs-string">&apos;XMLHttpRequest&apos;</span>;
    }
}
</code></pre>
<p>\test\extend\context.test.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test context&apos;</span>, () =&gt; {
    it(<span class="hljs-string">&apos;XHR is true&apos;</span>, () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext({
            <span class="hljs-attr">headers</span>: {
                <span class="hljs-string">&quot;X-Requested-With&quot;</span>: <span class="hljs-string">&quot;XMLHttpRequest&quot;</span>
            }
        });
        assert(ctx.isXHR == <span class="hljs-literal">true</span>);
    });

    it(<span class="hljs-string">&apos;XHR is true&apos;</span>, () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext({
            <span class="hljs-attr">headers</span>: {
                <span class="hljs-string">&quot;X-Requested-With&quot;</span>: <span class="hljs-string">&quot;SuperAgent&quot;</span>
            }
        });
        assert(ctx.isXHR == <span class="hljs-literal">false</span>);
    });
});
</code></pre>
<h4 id="t153.5.3 request">3.5.3 request <a href="#t153.5.3 request"> # </a></h4>
<p>app\extend\request.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    get isChrome() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.get(<span class="hljs-string">&apos;User-Agent&apos;</span>).toLowerCase().includes(<span class="hljs-string">&apos;chrome&apos;</span>);
    }
}
</code></pre>
<p>\test\extend\request.test.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test context&apos;</span>, () =&gt; {
    it(<span class="hljs-string">&apos;XHR is true&apos;</span>, () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext({
            <span class="hljs-attr">headers</span>: {
                <span class="hljs-string">&quot;X-Requested-With&quot;</span>: <span class="hljs-string">&quot;XMLHttpRequest&quot;</span>
            }
        });
        assert(ctx.isXHR == <span class="hljs-literal">true</span>);
    });

    it(<span class="hljs-string">&apos;XHR is true&apos;</span>, () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext({
            <span class="hljs-attr">headers</span>: {
                <span class="hljs-string">&quot;X-Requested-With&quot;</span>: <span class="hljs-string">&quot;SuperAgent&quot;</span>
            }
        });
        assert(ctx.isXHR == <span class="hljs-literal">false</span>);
    });
});
</code></pre>
<h4 id="t163.5.4 response">3.5.4 response <a href="#t163.5.4 response"> # </a></h4>
<p>app\extend\response.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    get isSuccess() {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.status == <span class="hljs-number">200</span>;
    }
}
</code></pre>
<p>\test\extend\response.test.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test app&apos;</span>, () =&gt; {
    it(<span class="hljs-string">&apos;response isSuccess is true&apos;</span>, () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext();
        ctx.status = <span class="hljs-number">200</span>;
        assert(ctx.response.isSuccess == <span class="hljs-literal">true</span>);
    });

    it(<span class="hljs-string">&apos;response isSuccess is false&apos;</span>, () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext();
        ctx.status = <span class="hljs-number">404</span>;
        assert(ctx.response.isSuccess == <span class="hljs-literal">false</span>);
    });
});
</code></pre>
<h4 id="t173.5.5 helper">3.5.5 helper <a href="#t173.5.5 helper"> # </a></h4>
<p>app\extend\helper.js</p>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = {
    money(val) {
        <span class="hljs-keyword">const</span> lang = <span class="hljs-keyword">this</span>.ctx.get(<span class="hljs-string">&apos;Accept-Language&apos;</span>);
        <span class="hljs-keyword">if</span> (lang.includes(<span class="hljs-string">&apos;zh-CN&apos;</span>)) {
            <span class="hljs-keyword">return</span> <span class="hljs-string">`&#xFFE5; <span class="hljs-subst">${val}</span>`</span>;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">return</span> <span class="hljs-string">`$ <span class="hljs-subst">${val}</span>`</span>;
        }
    }
}
</code></pre>
<p>\test\extend\helper.test.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> { app, assert } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;egg-mock/bootstrap&apos;</span>);
describe(<span class="hljs-string">&apos;test helper&apos;</span>, () =&gt; {
    it(<span class="hljs-string">&apos;should RMB&apos;</span>, () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext({
            <span class="hljs-attr">headers</span>: {
                <span class="hljs-string">&quot;Accept-Language&quot;</span>: <span class="hljs-string">&quot;zh-CN&quot;</span>
            }
        });
        assert(ctx.helper.money(<span class="hljs-number">100</span>) == <span class="hljs-string">&apos;&#xFFE5; 100&apos;</span>);
    });

    it(<span class="hljs-string">&apos;should Dollar&apos;</span>, () =&gt; {
        <span class="hljs-keyword">const</span> ctx = app.mockContext();
        assert(ctx.helper.money(<span class="hljs-number">100</span>) == <span class="hljs-string">&apos;$ 100&apos;</span>);
    });
});
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script data-cfasync="false" src="/cdn-cgi/scripts/5c5dd728/cloudflare-static/email-decode.min.js"></script><script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
